﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using LinkCard.Model;

namespace LinkCard.CustomerView.CusCp
{
    public partial class buy_glp_list : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";

        private int _totalRecords;

        private int nPageSize = 40;

        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        public Int64 customerID = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("login.aspx"); }
            if (Session["USERID"] == null) { Response.Redirect("login.aspx"); }

            customerID = Convert.ToInt64(Session["USERID"].ToString());
            if (!IsPostBack)
                LoadPayment();
        }

        private void LoadPayment()
        {
            var db = new TopLinkEntities();

            int curpage = 0;
            int skip = 0;

            if (Request.QueryString["page"] != null)
            {
                if (Request.QueryString["page"].ToString() != "1")
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                    skip = curpage * nPageSize;
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }
            }
            else
            {
                curpage = 1;
                skip = 0;
            }

            System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
            dateInfo.ShortDatePattern = "dd/MM/yyyy";

            if (sFromDate != "")
            {
                sFromDate += " 00:00:00";
                dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

            }
            if (sToDate != "")
            {
                sToDate += " 23:59:59";
                dToDate = Convert.ToDateTime(sToDate, dateInfo);
            }

            var _paylog = (from buymilelog in db.BuyMilesLog
                           from configmile in db.Config
                           where (buymilelog.CustomerID == customerID
                                 && buymilelog.ConfigVersion == configmile.Version
                                 && buymilelog.DateLog >= dFromDate
                                 && buymilelog.DateLog <= dToDate
                                 )
                           select new
                           {
                               buymilelog.MileToBuy,
                               buymilelog.DateLog,
                               buymilelog.Status,
                               configmile.PointToMile
                           }).OrderBy(t => t.DateLog).Skip(skip).Take(nPageSize);

            var paylog = _paylog.ToList();

            if (paylog.Count > 0)
            {
                _totalRecords = paylog.Count;

                int nPageCount;

                if (_totalRecords % nPageSize == 0)
                {
                    nPageCount = _totalRecords / nPageSize;
                }
                else
                {
                    nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                }

                if (nPageCount < 2) divPager.Visible = false;

                PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                msgList.DataSource = paylog;

                msgList.DataBind();
                msgNoData.Visible = false;
            }
            else
            {

                msgList.DataSource = null;
                msgList.DataSourceID = null;
                msgList.DataBind();
                msgNoData.Visible = true;
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value.Trim();
            sToDate = ToDate.Value.Trim();
            LoadPayment();
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var imgSucess = (HtmlImage)e.Item.FindControl("statusBuyImg");

                if (DataBinder.Eval(e.Item.DataItem, "Status").ToString() == "0")
                    imgSucess.Src = "Template/images/icons/exclamation.png";
                else
                    imgSucess.Src = "Template/images/icons/tick.png";
            }
        }
    }
}